import { DataSource, DataSourceOptions } from 'typeorm'
const path = require('path')

import { getConfig } from '../../utils/index'
const { MYSQL_DATABASE } = getConfig()
const DataBaseType: DataSourceOptions['type'] = MYSQL_DATABASE.type
const MYSQL_DATABASE_CONFIG = {
  ...MYSQL_DATABASE,
  type: DataBaseType,
  entities: [path.join(__dirname, `../../**/*.${MYSQL_DATABASE.entities}.entity{.ts,.js}`)]
}

const MYSQL_DATA_SOURCE = new DataSource(MYSQL_DATABASE_CONFIG);

export const DatabaseProviders = [
  {
    provide: "MYSQL_DATA_SOURCE",
    useFactory: async () => {
      if (!MYSQL_DATA_SOURCE.isInitialized) await MYSQL_DATA_SOURCE.initialize();
      return MYSQL_DATA_SOURCE;
    }
  }
]